package org.luxor.common.netty.jdk.bio.server;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * Java原生：阻塞I/O案例 服务端
 *
 * @author Mr.yan @date 2023/8/29
 */
public class TimeServer {
    public static void main(String[] args) throws IOException {
        int port = 8080;
        if (args != null && args.length > 0) {
            port = Integer.valueOf(args[0]);
        }
        ServerSocket server = null;
        try {
            server = new ServerSocket(port);
            System.out.println("The time server is start in port : " + port);

            Socket socket;
            while (true) {
                // 阻塞等待接收客户端指令
                socket = server.accept();
                System.out.println("The time server is start in port : " + port);

                // 每当有一个新的客户端请求接入时，服务端必须创建一个新的线程处理该客户端链路 (一个线程只能处理一个客户端连接).
                new Thread(new TimeServerHandler(socket)).start();
            }
        } finally {
            if (server != null) {
                System.out.println("The time server close");
                server.close();
            }
        }
    }
}
